<!--
 * @Author: tlm
 * @Date: 2020-06-28 07:53:21
 * @LastEditTime: 2020-07-01 16:29:21
 * @LastEditors: tlm
 * @Description: 
 * @FilePath: \S3DV_WipSurfer\src\components\view\tree\viewModelTree.vue
 * @support by tlm
--> 
<template>
  <div class='viewModelTree'>
      <el-input
      placeholder="输入树节点进行过滤"
      v-model="filterText">
      </el-input>
      <el-tree class='viewModelTree' :data='data' :highlight-current='true' :default-checked-keys="checkKeys" :filter-node-method="filterNode"
      :props="props"  node-key="uuid" show-checkbox @check-change="handleCheckChange" @node-click="handleNodeClick" 
      ref="viewModelTree" >
      </el-tree>
  </div>
</template>

<script>
import ComponentSignals from '@/utils/componentSignals';
export default {
  watch: {
      filterText(val) {
        this.$refs.viewModelTree.filter(val);
      }
    },
  name: 'viewModelTree',
  data(){
    return {
        filterText: '',
        data:[],
        checkKeys:[],
        uncheckKeys:[],
        props:
        {
            children: 'children',
            label: 'name'
        }
        
    }
  },
 
  components: {},
  created(){},
  mounted(){
      ComponentSignals.updateTree.add((data)=>{
          this.data=[data];
          this.filterText='';
          this.uncheckKeys=[];
          data.traverse((value)=>{
                this.checkKeys.push(value.uuid);
            });
      });
      ComponentSignals.clear.add(()=>{
        this.data=[];
        this.filterText='';
        this.checkKeys=[];
        this.uncheckKeys=[];
      });
  },
  methods: {
    filterNode(value, data) {
        if (!value) return true;
        return data.name.indexOf(value) !== -1;
    },

    handleNodeClick(data) {
        ComponentSignals.selectObjByUuid.dispatch(data.uuid);
      },
    handleCheckChange(data, checked, indeterminate) {
        if(indeterminate===false)
        {
          if(checked===false)
          {
            ComponentSignals.hideObjectByUuid.dispatch(data.uuid);
            this.uncheckKeys.push(data.uuid);
          }
          else{
            let index = this.uncheckKeys.indexOf( data.uuid );
            if(index !== - 1)
            {
              ComponentSignals.unhideObjectByUuid.dispatch(data.uuid);
              this.uncheckKeys.splice(index,1);
            }
          }
          
        }
       
      },
  }
}
</script>
<style scoped>
/* .viewModelTree
{
    background-color:transparent
} */
</style>
